const db = require("../db")

let addArticle = (req, res) => {
    if (!req.file || req.file.fieldname !== 'cover_img') return res.cc("文章封面是必填项")
    // 导入处理路径的 path 核心模块
    const path = require('path')
    const articleInfo = {
        ...req.body,
        cover_img: path.join("/uploads", req.file.filename),
        pub_date: new Date(),
        author_id:req.user.id
    }
    // console.log(articleInfo);
    const sql = "insert into ev_article set ?"
    db.query(sql, articleInfo, (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows !== 1) return res.cc("发布文章失败")
        res.cc("发布文章成功",0)
    })
}

module.exports = {
    addArticle,

}